/**
 * 使用 requestAnimationFrame 优化防抖事件
 */
const useThrottle = (cb: Function, frame: number = 1) => {
  let rafId: number | null = null
  let frameCount = -1
  return (...args: any): void => {
    if (rafId) cancelAnimationFrame(rafId)
    frameCount = ++frameCount % frame
    console.log(frameCount, frame)

    if (frameCount === 0)
      rafId = requestAnimationFrame(() => {
        cb.call(this, ...args)
        rafId = null
      })
  }
}
// 使用 setTimeout 节流
/* const useThrottle = (cb, delay) => {
    let timer = null
    return (...arg) => {
        if (timer) return
        timer = setTimeout(() => {
            cb.call(this, ...arg)
            timer = null
        }, delay)
    }
} */
export default useThrottle
